﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ChatIBOXClient
{
	public class Person
	{
		private string name;
		public string Name
		{
			get{return name;}
			set{name = value;}
		}
		
		public Person(string name)
		{
			this.name = name;
		}
	}
	/// <summary>
	/// Interaction logic for ListChatControl.xaml
	/// </summary>
	/// 
	public partial class ListChatControl : UserControl
	{
        ProxySingleton proxySingleton = ProxySingleton.GetInstance();

		public ListChatControl()
		{
            this.InitializeComponent();
			Person person = new Person("Khoi");
            List<Person> listPerson = new List<Person>();
            listPerson.Add(new Person("Khoi"));
            listPerson.Add(new Person("Hien"));
            listPerson.Add(new Person("Tuyen"));
            listPerson.Add(new Person("ac"));
            listPerson.Add(new Person("Khoi"));
            listPerson.Add(new Person("Hien"));
            listPerson.Add(new Person("Tuyen"));
            listPerson.Add(new Person("ac"));
            listPerson.Add(new Person("Khoi"));
            listPerson.Add(new Person("Hien"));
            listPerson.Add(new Person("Tuyen"));
            listPerson.Add(new Person("ac"));
            listPerson.Add(new Person("Khoi"));
            listPerson.Add(new Person("Hien"));
            listPerson.Add(new Person("Tuyen"));
            listPerson.Add(new Person("ac"));
            listPerson.Add(new Person("Khoi"));
            listPerson.Add(new Person("Hien"));
            listPerson.Add(new Person("Tuyen"));
            listPerson.Add(new Person("ac"));
            listPerson.Add(new Person("Khoi"));
            listPerson.Add(new Person("Hien"));
            listPerson.Add(new Person("Tuyen"));
            listPerson.Add(new Person("ac"));
            listPerson.Add(new Person("Khoi"));
            listPerson.Add(new Person("Hien"));
            listPerson.Add(new Person("Tuyen"));
            listPerson.Add(new Person("ac"));

            
			//lstListChatter.Items.Add(person);
			lstListChatter.DataContext = listPerson;
			
			
            //ListBoxItem list1 = new ListBoxItem();
            //list1.Content = "khoi";
            
            ////list1.MouseDoubleClick += new MouseButtonEventHandler(listboxChatter1_MouseDoubleClick);
            //this.listboxChat.Items.Add(list1);
            proxySingleton.ProxyCallbackEvent += new ProxySingleton.ProxyCallbackEventHander(this.ProxySingleton_ProxyCallbackEvent);
		}

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            lstListChatter.MouseDoubleClick += new MouseButtonEventHandler(lstListChatter_MouseDoubleClick);
        }

        protected void lstListChatter_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ChatWindow chatWindow = new ChatWindow();
            chatWindow.Visibility = Visibility.Visible;
            chatWindow.Title = ((Person)lstListChatter.SelectedValue).Name;
        }

        public void ProxySingleton_ProxyCallbackEvent(object sender, ProxyCallbackEventArgs e)
        {
            switch (e.callbackType)
            {
                case CallbackType.UserJoin:
                    UserJoin(e.client);
                    break;
                case CallbackType.UserLeave:
                    break;
                case CallbackType.RefreshClient:
                    this.RefreshClient(e.listClient);
                    break;
                case CallbackType.HasNotify:
                    break;
                default:
                    break;
            }
            //this.textboxPwd.Password = e.message.Content;
        }

        

        // 
        private void RefreshClient(ServiceReference.Client [] listClient)
        {
            this.listboxChat.Items.Clear();
            foreach (ServiceReference.Client client in listClient)
            {
                ListBoxItem lbItem = new ListBoxItem();
                lbItem.Content = client.Name;
                lbItem.Height = 25;
                lbItem.BorderBrush = Brushes.Black;
                if (client.Status == 0)
                {
                    lbItem.Foreground = Brushes.Gray;
                }
                
                this.listboxChat.Items.Add(lbItem);
            }
        }

        // 
        private void UserJoin(ServiceReference.Client client)
        {
            ListBoxItem item = new ListBoxItem();
            item.Content = client.Name;
            //this.listboxChat.Items.Add(item);
        }
        private void ListBoxItem_Selected(object sender, RoutedEventArgs e)
        {
           // SendMessageWindow sendMessageWindow = new SendMessageWindow();
//            sendMessageWindow.Visibility = Visibility.Visible;
            //sendMessageWindow.Title = listboxChatter.Content.ToString();
           // ListBoxItem lb = e.OriginalSource as ListBoxItem;
           // sendMessageWindow.Title = lb.Content.ToString();
            //new SendMessageWindow().Visibility = Visibility.Visible;
        }

        private void listboxChatter1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            //SendMessageWindow sendMessageWindow = new SendMessageWindow();
            //sendMessageWindow.Visibility = Visibility.Visible;
            //ListBoxItem lbitem = ItemsControl.ContainerFromElement(listboxChat, e.OriginalSource as DependencyObject) as ListBoxItem;
            //sendMessageWindow.Title = lbitem.Content.ToString();

            ChatWindow chatWindow = new ChatWindow();
            chatWindow.Visibility = Visibility.Visible;
            ListBoxItem lbitem = ItemsControl.ContainerFromElement(listboxChat, e.OriginalSource as DependencyObject) as ListBoxItem;
            //chatWindow.Receiver.Name = lbitem.Content.ToString();
            chatWindow.Receiver.Name = lbitem.Content.ToString();
            chatWindow.Title = "Chatting with " + chatWindow.Receiver.Name.ToString();
            MainWindow.listChatting.Add(chatWindow.Receiver.Name, chatWindow);

            //Button btn = lbitem.Content as Button;
            //sendMessageWindow.Title = btn.Content.ToString();
        }
	}
}